package com.vng.zini.gifapp.activity;

import java.util.ArrayList;

import com.vng.zini.gifapp.components.Images;
import com.vng.zini.gifapp.config.AppParameters;
import com.vng.zini.giffapp.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class ImagePreviewActivity extends Activity {
	private ArrayList<Bitmap> listBitmap;
	private int count;
	private ImageAdapter imageAdapter;
	private Bitmap overlay;
	private ImageButton btn_next,btn_back;
	private boolean imageChecked[];
	private ArrayList<Bitmap> listImageChecked;
	private int numImageChecked;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		System.out.println("on create test image preview");
		setContentView(R.layout.image_preview);
		
		settingParameter();
		setButtonListener();
		GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
		imageAdapter = new ImageAdapter();
		imagegrid.setAdapter(imageAdapter);

	}
	private void settingParameter(){
		listBitmap = Images.listImage;
		overlay = BitmapFactory.decodeResource(getResources(),
				R.drawable.overlay);
		count = listBitmap.size();
		numImageChecked = count;
		//count = 40;
		imageChecked = new boolean[count];
		for(int i=0;i<count;i++){
			imageChecked[i] = true;
		}
	}
	
	private void configParamsBeforeSending(){
		AppParameters.imageChecked = imageChecked;
		listImageChecked = new ArrayList<Bitmap>();
		int numberChecked = 0;
		for(int i=0;i<count;i++){
			if(imageChecked[i]){
				listImageChecked.add(Images.listImage.get(i));
				numberChecked ++;
			}
		}
		Images.listImageChecked = listImageChecked;
		AppParameters.numberFramesChecked = numberChecked;
		System.out.println("number image checked");
		System.out.println(numberChecked);
	}
	private void setButtonListener(){
		btn_next = (ImageButton) findViewById(R.id.topbar_black_right_btn_image_preview);
		btn_back = (ImageButton) findViewById(R.id.topbar_black_left_btn_image_preview);
		
		
		btn_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(ImagePreviewActivity.this, CameraActivity.class);
				startActivity(intent);
				finish();
			}
		});
		
		btn_next.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				configParamsBeforeSending();
				Intent intent = new Intent(ImagePreviewActivity.this, GIFPreviewActivity.class);
				startActivity(intent);
				
			}
		});
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		System.out.println("on start test image preview");
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		System.out.println("on restart test image preview");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		System.out.println("on resume test image preview");
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		System.out.println("on pause test image preview");
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		System.out.println("on stop test image preview");
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		System.out.println("on destroy test image preview");
	}

	public class ImageAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public ImageAdapter() {
			mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			return count;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolder holder;
			System.out.print("position:");
			System.out.println(position);
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.galleryitem, null);
				holder.imageview = (ImageView) convertView
						.findViewById(R.id.thumbImage);
				holder.overlay = (ImageView) convertView.findViewById(R.id.mask);
				
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			//holder.checkbox.setId(position);
			holder.imageview.setId(position);
			holder.overlay.setId(position);
			holder.imageview.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					numImageChecked ++;
					//holder.overlay.bringToFront();
					holder.overlay.setVisibility(View.VISIBLE);
					//holder.overlay.requestLayout();
					imageChecked[holder.imageview.getId()] = true;
					System.out.println(holder.imageview.getId());
					
				}
			});

			holder.overlay.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					if(numImageChecked==1){
						Toast.makeText(ImagePreviewActivity.this, "Chọn ít nhất 1 ảnh",
								Toast.LENGTH_SHORT).show();
						return;
					}
					numImageChecked --;
					//holder.imageview.bringToFront();
					holder.overlay.setVisibility(View.INVISIBLE);
					//holder.imageview.requestLayout();
					imageChecked[holder.imageview.getId()] = false;
					System.out.println(holder.imageview.getId());
				}
			});

			holder.imageview.setImageBitmap(listBitmap.get(position));
			holder.overlay.setImageBitmap(overlay);
			if(imageChecked[position]){
				holder.overlay.setVisibility(View.VISIBLE);
			}
			else{
				holder.overlay.setVisibility(View.INVISIBLE);
			}
			holder.id = position;
			return convertView;
		}
	}
	private void showCheckeck(){
		for(int i=0;i<count;i++){
			System.out.println(imageChecked[i]);
		}
	}
	
	class ViewHolder {
		ImageView imageview;
		ImageView overlay;
		//CheckBox checkbox;
		int id;
	}

}
